package com.kikt.viewpagerindicationdemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.kikt.viewpagerindicationdemo.fragment.TextFragment;
import com.kikt.viewpagerindicationdemo.lib.TitlePageIndicator;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends FragmentActivity {

    @Bind(R.id.vp)
    ViewPager mVp;
    protected FragmentPagerAdapter fragmentPagerAdapter;
    protected List<Fragment> list;

    String[] arr = {"标题1", "标题2", "标题3"};
    @Bind(R.id.indicator)
    TitlePageIndicator mIndicator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initAdapter();
        mVp.setAdapter(fragmentPagerAdapter);
        mIndicator.setViewPager(mVp);
    }

    private void initAdapter() {
        list = new ArrayList<>();

        for (int i = 0; i < 3; i++) {
            list.add(TextFragment.newInstance(arr[i]));
        }

        fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {

            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return arr[position];
            }
        };
    }

}
